﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using UtilPublic.ServerLoger;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using UtilLib;
using UtilPublic.Serialization;
using System.Linq;

namespace UtilPublic.Redis
{
    /// <summary>
    /// Hash存储
    /// </summary>
    public partial class Rds
    {
        /// <summary>
        /// 设置
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <typeparam name="T3"></typeparam>
        /// <param name="key"></param>
        /// <param name="hashField"></param>
        /// <param name="value"></param>
        /// <param name="jobj"></param>
        /// <returns></returns>
        public bool HashSet<T1, T2, T3>(T1 key, T2 hashField, T3 value, bool jobj = false)
            => RedisDb.HashSet(Zk(key), $"{hashField}", ToText(value, jobj));
        /// <summary>
        /// 批量设置
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <typeparam name="T3"></typeparam>
        /// <param name="key"></param>
        /// <param name="dic"></param>
        /// <param name="jobj"></param>
        /// <returns></returns>
        public void HashSetDict<T1, T2, T3>(T1 key, Dictionary<T2, T3> dic, bool jobj = false)
        {
            foreach (var kvp in dic)
            {
                RedisDb.HashSet(Zk(key), $"{kvp.Key}", ToText(kvp.Value, jobj));
            }
        }
        /// <summary>
        /// 获取
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="key"></param>
        /// <param name="hashField"></param>
        /// <returns></returns>
        public RedisValue HashGet<T1, T2>(T1 key, T2 hashField) => RedisDb.HashGet(Zk(key), $"{hashField}");
        /// <summary>
        /// 获取
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <typeparam name="T3"></typeparam>
        /// <param name="key"></param>
        /// <param name="hashField"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool HashGet<T1, T2, T3>(T1 key, T2 hashField, out T3 value)
            where T3 : new()
        {
            try
            {
                value = ToJobj<T3>(RedisDb.HashGet(Zk(key), $"{hashField}"));
                return true;
            }
            catch
            {
                value = new();
                return false;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="key"></param>
        /// <param name="hashField"></param>
        /// <returns></returns>
        public bool HashDelete<T1, T2>(T1 key, T2 hashField) => RedisDb.HashDelete(Zk(key), $"{hashField}");
        /// <summary>
        /// 删除
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="key"></param>
        /// <param name="list"></param>
        /// <returns></returns>
        public long HashDelete<T1, T2>(T1 key, List<T2> list)
        {
            RedisValue[] hashFields = new RedisValue[list.Count];
            for (int i = 0; i < list.Count; i++)
            {
                hashFields[i] = $"{list[i]}";
            }
            return RedisDb.HashDelete(Zk(key), hashFields);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public long HashDeleteAll<T1>(T1 key)
        {
            return RedisDb.HashDelete(Zk(key), HashKeys(key));
        }
        /// <summary>
        /// 是否存在
        /// </summary>
        /// <typeparam name="T1"></typeparam>
        /// <typeparam name="T2"></typeparam>
        /// <param name="key"></param>
        /// <param name="hashField"></param>
        /// <returns></returns>
        public bool HashExists<T1, T2>(T1 key, T2 hashField) => RedisDb.HashExists(Zk(key), $"{hashField}");
        /// <summary>
        /// 长度
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public long HashLength<T>(T key) => RedisDb.HashLength(Zk(key));
        /// <summary>
        /// 获取全部Keys
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public RedisValue[] HashKeys<T>(T key) => RedisDb.HashKeys(Zk(key));
        /// <summary>
        /// 获取全部Hash 慎用
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public RedisValue[] HashValues<T>(T key) => RedisDb.HashValues(Zk(key));
        /// <summary>
        /// 获取全部Hash 慎用
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public HashEntry[] HashGetAll<T>(T key) => RedisDb.HashGetAll(Zk(key));
        /// <summary>
        /// 获取全部Hash 慎用
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="result"></param>
        /// <returns></returns>
        public bool HashValues<T>(string key, out List<T> result)
        {
            result = new();
            try
            {
                var list = RedisDb.HashValues(Zk(key));
                foreach (var item in list)
                {
                    result.Add(Jsh.Jobj<T>(item));
                }
                return true;
            }
            catch
            {
                Zloger.Fatal($"HashValues Error {key}");
                return false;
            }
        }

    }
}